﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Reporting.WinForms;

namespace SSRS_Plus
{
    public partial class frmDocumentation : Form
    {
        public frmDocumentation()
        {
            InitializeComponent();
        }

        private void frmDocumentation_Load(object sender, EventArgs e)
        {
            lblServerName.Text = ReportWrapper.ActiveServer.ServerName + " - " + ReportWrapper.ActiveServer.ServerURL;
            this.rptvDoc.RefreshReport();
        }

        private void btnGenerateDocumentation_Click(object sender, EventArgs e)
        {
            try
            {
                GenerateReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GenerateReport()
        {
            try
            {
                this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
                //reset the report viewer
                this.rptvDoc.Reset();

                //set processing to local mode
                this.rptvDoc.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;

                DataSet dsRpt =  new DataSet("dsReport");

                dsRpt.Tables.Add("dtReport");

                //load .rdlc file and add a datasource
                this.rptvDoc.LocalReport.ReportPath = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "") + @"Report\rptDocumentation.rdlc";
                //this.rptvDoc.LocalReport.ReportPath = @"C:\Projects\SSRS Plus\SSRS Plus\SSRS Plus\bin\Debug\Report\rptDocumentation.rdlc";

                this.rptvDoc.LocalReport.DataSources.Add(GetReportData());

                this.rptvDoc.LocalReport.EnableHyperlinks = true;

                ReportParameter[] param = new ReportParameter[1];
                param[0] = new ReportParameter("ReportServer", ReportWrapper.ActiveServer.ServerName, false);

                this.rptvDoc.LocalReport.SetParameters(param);

                this.rptvDoc.LocalReport.DisplayName = "SSRS Objects Detail Report";

                this.rptvDoc.Visible = true;
                this.rptvDoc.RefreshReport();

                this.Cursor = System.Windows.Forms.Cursors.Arrow;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = System.Windows.Forms.Cursors.Arrow;
            }
        }

        private ReportDataSource GetReportData()
        {
            try
            {
                ReportDataSource ds1 = new ReportDataSource("dsReport_dtReport", ReportWrapper.GetReportData());

                return ds1;
            }
            catch
            {
                throw;
            }
        }
    }
}
